package com.lhy.admin.service;

import com.lhy.admin.common.mvcplus.entity.BasePage;
import com.lhy.admin.common.mvcplus.entity.RespPage;
import com.lhy.admin.common.mvcplus.service.SimpleService;
import com.lhy.admin.mapper.DictMapper;
import com.lhy.admin.model.Dict;
import com.lhy.admin.model.extend.DictAttribute;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@Slf4j
public class DictService extends SimpleService<DictMapper, Dict> {


    public RespPage<Dict> list(BasePage basePage, Dict dict) {
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Dict> page = baseMapper.list(toPlusPage(basePage), dict);
        return toRespPage(basePage, page);
    }


    public void updateDict(DictAttribute bean) {
        //删除全部
        if (bean.getId() != null) {
            this.remove(new Dict().setCode(bean.getCode()));
        }
        //再新增
        List<Dict> attributes = bean.getAttributes();
        if (!CollectionUtils.isEmpty(attributes)) {
            attributes.stream()
                    .map(dictAttributes -> dictAttributes.setCode(bean.getCode()).setDescription(bean.getDescription()).setStatus(1))
                    .forEach(this::save);
        }
    }


}
